package com.afkt.project.ui.slice;

import com.afkt.project.model.bean.MyListBean;
import com.afkt.project.ui.adapter.MyListAdapter;
import com.dev.receiver.*;
import com.dev.utils.*;
import com.dev.utils.app.assist.ScreenSensorAssist;

import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.ListContainer;
import ohos.agp.window.dialog.ToastDialog;
import ohos.sensor.agent.CategoryOrientationAgent;
import ohos.sensor.bean.CategoryOrientation;
import ohos.sensor.data.CategoryOrientationData;
import ohos.sensor.listener.ICategoryOrientationDataCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 事件/广播监听的slice
 */
public class ListenerSlice extends AbilitySlice {

    private ListContainer listContainer;
    private MyListAdapter mAdapter;

    WifiEventSubscriber subscriber;

    MyRadioStateObserver observer;

    BatterySubscriber batterySubscriber;

    TimeSubscriber timeSubscriber;

    PackageSubscriber packageSubscriber;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_layout_Listener);
        //注册wifi监听
        subscriber = WifiUtils.initMatch(ListenerSlice.this);
        //执行回调的runner；创建MyRadioStateObserver的对象；添加回调
        observer = NetWorkUtils.initNetWork(ListenerSlice.this);
        // 创建传感器回调对象
        ScreenSensorAssist.initOritation(ListenerSlice.this);
        //注册电池监听
        batterySubscriber = BatteryUtils.initMatch(ListenerSlice.this);
        //注册时间监听
        timeSubscriber = TimeUtils.initMatch(ListenerSlice.this);
        //注册应用监听
        packageSubscriber = PackageUtils.initMatch(ListenerSlice.this);
        //初始化页面button列表
        initList();

    }

    private void initList() {
        List<MyListBean> list = initData();
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listener_list);
        mAdapter = new MyListAdapter(list, this);
        listContainer.setItemProvider(mAdapter);
        listContainer.setItemClickedListener((listContainer, component, position, l) -> {
            switch (position) {
                case 0:
                    new ToastDialog(getContext()).setText("Wifi监听成功,请查看Logcat")
                            .show();
                    WifiUtils.subscribeWifi(subscriber);
                    break;
                case 1:
                        new ToastDialog(getContext()).setText("网络监听成功，请查看Logcat")
                                .show();
                        NetWorkUtils.subscribeNetWork(observer);
                    break;
                case 2:
                    new ToastDialog(getContext()).setText("暂不支持")
                            .show();
                    break;
                case 3:
                    new ToastDialog(getContext()).setText("暂不支持")
                            .show();
                case 4:
                    new ToastDialog(getContext()).setText("时区、时间监听成功，请查看Logcat")
                            .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                            .show();
                    TimeUtils.subscriberTime(timeSubscriber);
                    break;
                case 5:
                    new ToastDialog(getContext()).setText("屏幕监听成功，请查看Logcat")
                            .show();
                    ScreenUtils.subscribeScreen(ListenerSlice.this);
                    break;
                case 6:
                        new ToastDialog(getContext()).setText("屏幕旋转监听成功(重力传感器)，请查看Logcat")
                                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                                .show();
                        ScreenSensorAssist.subscribeOritation();
                    break;
                case 7:
                        new ToastDialog(getContext()).setText("屏幕旋转监听成功，请查看Logcat")
                                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                                .show();
                        ScreenSensorAssist.subscribeOritation();
                    break;
                case 8:
                    new ToastDialog(getContext()).setText("电量监听成功，请查看Logcat")
                            .show();
                    BatteryUtils.subscriberBattery(batterySubscriber);
                    break;
                case 9:
                        new ToastDialog(getContext()).setText("应用状态监听成功，请查看Logcat")
                                .setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                                .show();
                        PackageUtils.subscriberPackage(packageSubscriber);
                    break;
                default:
                    break;
            }
        });

        listContainer.setItemLongClickedListener((listContainer, component, position, l) -> {
            switch (position) {
                case 0:
                    new ToastDialog(getContext()).setText("注销Wifi监听成功").show();
                    WifiUtils.unSubscribeWifi(subscriber);
                    break;
                case 1:
                    new ToastDialog(getContext()).setText("注销网络监听成功").show();
                    NetWorkUtils.unsubscribeNetWork(observer);
                    break;
                case 3:
                    new ToastDialog(getContext()).setText("注销短信监听成功")
                            .show();
                    break;
                case 4:
                    new ToastDialog(getContext()).setText("注销时区时间监听成功")
                            .show();
                    TimeUtils.unSubscribeTime(timeSubscriber);
                    break;
                case 5:
                    new ToastDialog(getContext()).setText("注销屏幕监听成功")
                            .show();
                    ScreenUtils.unSubscribeScreen();
                    break;
                case 6:
                    new ToastDialog(getContext()).setText("注销屏幕旋转监听成功（重力传感器）")
                            .show();
                    ScreenSensorAssist.unSubscribeOritation();
                    break;
                case 7:
                    new ToastDialog(getContext()).setText("注销屏幕旋转监听成功")
                            .show();
                    ScreenSensorAssist.unSubscribeOritation();
                    break;
                case 8:
                    new ToastDialog(getContext()).setText("注销电量监听成功")
                            .show();
                    BatteryUtils.unSubscribeBattery(batterySubscriber);
                    break;
                case 9:
                    new ToastDialog(getContext()).setText("注销应用状态监听成功")
                            .show();
                    PackageUtils.unSubscribePackage(packageSubscriber);
                    break;
                default:
                    break;
            }
            return false;
        });
    }

    private List<MyListBean> initData() {
        List<MyListBean> lists = new ArrayList<>();
        lists.add(new MyListBean("Wifi监听"));
        lists.add(new MyListBean("网络监听"));
        lists.add(new MyListBean("电话监听"));
        lists.add(new MyListBean("短信监听"));
        lists.add(new MyListBean("时区、时间监听"));
        lists.add(new MyListBean("屏幕监听"));
        lists.add(new MyListBean("屏幕旋转监听(重力传感器)"));
        lists.add(new MyListBean("屏幕旋转监听"));
        lists.add(new MyListBean("电量监听"));
        lists.add(new MyListBean("应用状态监听"));
        return lists;
    }


    //重力传感器相关
    private static final long INTERVAL = 100000000L;
    private CategoryOrientationAgent categoryOrientationAgent = new CategoryOrientationAgent();
    private ICategoryOrientationDataCallback orientationDataCallback;
    private CategoryOrientation orientationSensor;
    private int matrix_length = 9;
    private int rotationVectorLength = 9;

    private void initOritation() {
        // 创建传感器回调对象
        orientationDataCallback = new ICategoryOrientationDataCallback() {
            @Override
            public void onSensorDataModified(CategoryOrientationData categoryOrientationData) {
                // 对接收的categoryOrientationData传感器数据对象解析和使用
                int dim = categoryOrientationData.getSensorDataDim(); // 获取传感器的维度信息
                float degree = categoryOrientationData.getValues()[0]; // 获取方向类传感器的第一维数据
                float[] rotationMatrix = new float[matrix_length];
                CategoryOrientationData.getDeviceRotationMatrix(rotationMatrix, categoryOrientationData.values); // 根据旋转矢量传感器的数据获得旋转矩阵
                float[] rotationAngle = new float[rotationVectorLength];
                rotationAngle = CategoryOrientationData.getDeviceOrientation(rotationMatrix, rotationAngle); // 根据计算出来的旋转矩阵获取设备的方向
            }

            @Override
            public void onAccuracyDataModified(CategoryOrientation categoryOrientation, int index) {
                // 使用变化的精度
            }

            @Override
            public void onCommandCompleted(CategoryOrientation categoryOrientation) {
                // 传感器执行命令回调
                new ToastDialog(getContext()).setText("屏幕旋转").show();
                LogUtils.info("Oritation", "屏幕发生旋转！");
            }
        };
    }

    /**
     * 订阅传感器数据
     */
    private void oritation() {
        // 获取传感器对象，并订阅传感器数据
        orientationSensor = categoryOrientationAgent.getSingleSensor(
                CategoryOrientation.SENSOR_TYPE_SCREEN_ROTATION);
        if (orientationSensor != null) {
            categoryOrientationAgent.setSensorDataCallback(
                    orientationDataCallback, orientationSensor, INTERVAL);
        }
    }
}
